<?php
/*
 * Created on 21 janv. 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */
class Model
{
	protected $params;
	protected $menuG;
	protected $daoFactory;
	protected $footer;
	protected $header;
	protected $BreadCrumb;
	protected $languagePad;
	private $catPath;
	private $isPrepared = false;
	private $usrPad;
	protected $translator;
	function __construct($p,$translator)
	{
		$this->translator = $translator;
		$this->params=$p;
		$this->daoFactory = new DAOFactory();
		if(!is_null($this->translator)){
			$this->menuG = new MenuTreeGenerator($this->translator,$this->params,$this->daoFactory);
			$this->BreadCrumb = new BreadCrumbGenerator($this->translator,$this->params,$this->daoFactory);
			$this->languagePad = new LanguagePadGenerator($this->translator,$this->params,$this->daoFactory);
			$this->usrPad = new UserPadGenerator($this->translator,$this->params,$this->daoFactory);
		}
	}
	public function getFactory(){
		return $this->daoFactory;
	}
	private function prepareBreadCrumb(){
		if (!$this->isPrepared){
			$this->BreadCrumb->prepareData();
			$this->catpath = $this->BreadCrumb->getCategoryPath();
			$this->isPrepared = true;
		}
	}
	public function isAncestorCategory($catId){
		$this->prepareBreadCrumb();
		$bool=false;
		if (in_array($catId, $this->catpath)){
			$bool=true;
		}
		return $bool;
	}
	function getLanguageIsoCookyValue(){
			
		if (!isset($_COOKIE[$_SESSION['config']->sitename]['langue'])||is_null($_COOKIE[$_SESSION['config']->sitename]['langue']) || $_COOKIE[$_SESSION['config']->sitename]['langue'] == ''){
			$tmp = null;
		}else{
			$tmp = $_COOKIE[$_SESSION['config']->sitename]['langue'];
		}
		return $tmp;
	}
	function getDefaultLanguageIso(){
		$lngDAO = $this->daoFactory->getLangueDAO();
		$lng = $lngDAO->getDefaultLanguage();
		return $lng->getIso();
	}
	function getMenuTree(){
		return $this->menuG->dump();
	}
	function getLanguagePad(){
		return $this->languagePad->dump();
	}
	function getUserPad(){
		return $this->usrPad->dump();
	}
	function getBreadCrumb(){
		$this->prepareBreadCrumb();
		return $this->BreadCrumb->dump();
	}
	function getAnonymousUser(){

		return $this->daoFactory->getUserDAO()->getAnonymousUser();
	}
	function getLangues(){
		return $this->daoFactory->getLangueDAO()->all();
	}
	function startFormSession(){
		if(!isset($_SESSION['FormManager']) || !is_a($_SESSION['FormManager'], 'FormManager')){
			$_SESSION['FormManager'] = new FormManager();
		}
		$_SESSION['FormManager']->start();
	}
	function stopFormSession(){
		if(isset($_SESSION['FormManager']) && is_a($_SESSION['FormManager'], 'FormManager')){
			$_SESSION['FormManager']->stop();
		}
	}
	function addFormError($Exception){
		$_SESSION['FormManager']->addError($Exception);
	}
	function addFormValue($fieldId, $fieldValue){
		$_SESSION['FormManager']->addFormContent($fieldId,$fieldValue);
	}
	function hasFormError(){
		if(isset($_SESSION['FormManager']) && is_a($_SESSION['FormManager'], 'FormManager')){
			$_SESSION['FormManager']->hasError();
		}
	}
	function updateUserSecInfo(){
		$usrDAO = $this->daoFactory->getUserDAO();
		$usrDAO->update($_SESSION['USER_BEAN']);
	}
}
?>